/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package it.coraci.anag.gui.docs;

import it.coraci.anag.db.mongo.controllers.MongoDocumentJpaController;
import it.coraci.anag.db.mongo.entities.Attribute;
import it.coraci.anag.db.mongo.entities.MongoDocument;
import it.coraci.anag.db.sql.controllers.IDDocTypeJpaController;
import it.coraci.anag.db.sql.controllers.PersonJpaController;
import it.coraci.anag.db.sql.entities.IDDocType;
import it.coraci.anag.db.sql.entities.IDDocument;
import it.coraci.anag.db.sql.entities.Person;
import java.beans.Beans;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.Persistence;

/**
 *
 * @author Luca
 */
public class DocumentManagerPanel extends javax.swing.JPanel {
    
    private Person person = null;
    private List<ParamPanel> params = new ArrayList<>();
    MongoDocumentJpaController mongoDocumentJPAcontroller ;

    /**
     * Creates new form DocumentManager
     */
    public DocumentManagerPanel() {
        initComponents();
        if (!Beans.isDesignTime()) {
            mongoDocumentJPAcontroller= new MongoDocumentJpaController(Persistence.createEntityManagerFactory("nosqlDBPU"));
            IDDocTypeJpaController cc = new IDDocTypeJpaController(Persistence.createEntityManagerFactory("dbAnagBasePU"));
            List<IDDocType> types = cc.findIDDocTypeEntities();
            for (IDDocType iDDocType : types) {
                this.docTypeComboBoxModel1.addElement(iDDocType);
            }
            PersonJpaController cc2 = new PersonJpaController(Persistence.createEntityManagerFactory("dbAnagBasePU"));
            List<Person> findPersonEntities = cc2.findPersonEntities();
            for (Person p : findPersonEntities) {
                this.peopleListModel1.addElement(p);
            }
        }
    }

    public void setPerson(Person person) {
        this.person = person;
    }
    
    

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        docTypeComboBoxRenderer1 = new it.coraci.anag.gui.docs.mr.DocTypeComboBoxRenderer();
        docTypeComboBoxModel1 = new it.coraci.anag.gui.docs.mr.DocTypeComboBoxModel();
        peopleListModel1 = new it.coraci.anag.gui.people.mr.PeopleListModel();
        peopleListRenderer1 = new it.coraci.anag.gui.people.mr.PeopleListRenderer();
        documentComboBoxModel1 = new it.coraci.anag.gui.docs.mr.DocumentComboBoxModel();
        documentComboBoxRenderer1 = new it.coraci.anag.gui.docs.mr.DocumentComboBoxRenderer();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();

        docTypeComboBoxRenderer1.setText("docTypeComboBoxRenderer1");

        peopleListRenderer1.setText("peopleListRenderer1");

        documentComboBoxRenderer1.setText("documentComboBoxRenderer1");

        jLabel1.setText("Documenti:");

        jComboBox1.setModel(documentComboBoxModel1);
        jComboBox1.setRenderer(documentComboBoxRenderer1);
        jComboBox1.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBox1ItemStateChanged(evt);
            }
        });

        jButton1.setText("Salva");

        jList1.setModel(peopleListModel1);
        jList1.setCellRenderer(peopleListRenderer1);
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(jList1);

        jButton2.setText("Add New Document");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel1.setBackground(new java.awt.Color(102, 255, 51));
        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.Y_AXIS));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 181, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton1))
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton2)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addContainerGap())
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 384, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
       NewDocFrame frame = new NewDocFrame();
       frame.setLocationRelativeTo(this);
       frame.setSelectedPerson(person);
       frame.setVisible(true);
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        Object selectedValue = jList1.getSelectedValue();
        if(selectedValue!=null && selectedValue instanceof Person){
            this.person = ((Person)selectedValue);
            this.documentComboBoxModel1.removeAllElements();
            List<IDDocument> iddocuments = ((Person)selectedValue).getIddocuments();
            for (IDDocument iDDocument : iddocuments) {
                this.documentComboBoxModel1.addElement(iDDocument);
            }
            
            
        }
    }//GEN-LAST:event_jList1ValueChanged

    private void jComboBox1ItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBox1ItemStateChanged
        Object selectedItem = this.jComboBox1.getSelectedItem();
        if(selectedItem!=null && selectedItem instanceof IDDocument){
            for (ParamPanel paramPanel : params) {
                this.jPanel1.remove(paramPanel);
            }
            String fileID = ((IDDocument)selectedItem).getFileID();
            MongoDocument findMongoDocument = mongoDocumentJPAcontroller.findMongoDocument(fileID);
            List<Attribute> values = findMongoDocument.getValues();
            for (Attribute attribute : values) {
                ParamPanel paramPanel = new ParamPanel(attribute);
                this.params.add(paramPanel);
                this.jPanel1.add(paramPanel);
            }
            this.jPanel1.invalidate();
            this.jPanel1.revalidate();
            this.jPanel1.repaint();
            
        }
    }//GEN-LAST:event_jComboBox1ItemStateChanged


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private it.coraci.anag.gui.docs.mr.DocTypeComboBoxModel docTypeComboBoxModel1;
    private it.coraci.anag.gui.docs.mr.DocTypeComboBoxRenderer docTypeComboBoxRenderer1;
    private it.coraci.anag.gui.docs.mr.DocumentComboBoxModel documentComboBoxModel1;
    private it.coraci.anag.gui.docs.mr.DocumentComboBoxRenderer documentComboBoxRenderer1;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JList jList1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private it.coraci.anag.gui.people.mr.PeopleListModel peopleListModel1;
    private it.coraci.anag.gui.people.mr.PeopleListRenderer peopleListRenderer1;
    // End of variables declaration//GEN-END:variables
}
